package com.kxmall.web.controller.huishou;

import java.util.List;
import java.util.Arrays;

import com.kxmall.common.enums.BillDetailEnum;
import com.kxmall.common.enums.BillEnum;
import com.kxmall.huishou.domain.HsWithdrawRecord;
import com.kxmall.user.domain.bo.KxUserBillBo;
import com.kxmall.user.domain.bo.KxUserBo;
import com.kxmall.user.domain.vo.KxUserVo;
import com.kxmall.web.controller.user.service.IKxUserBillService;
import com.kxmall.web.controller.user.service.IKxUserService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.checkerframework.checker.units.qual.K;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.kxmall.common.annotation.RepeatSubmit;
import com.kxmall.common.annotation.Log;
import com.kxmall.common.core.controller.BaseController;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.validate.AddGroup;
import com.kxmall.common.core.validate.EditGroup;
import com.kxmall.common.enums.BusinessType;
import com.kxmall.common.utils.poi.ExcelUtil;
import com.kxmall.huishou.domain.vo.HsWithdrawRecordVo;
import com.kxmall.huishou.domain.bo.HsWithdrawRecordBo;
import com.kxmall.huishou.service.IHsWithdrawRecordService;
import com.kxmall.common.core.page.TableDataInfo;

/**
 * 用户提现记录
 *
 * @author lyrics
 * @date 2023-12-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/huishou/withdrawRecord")
public class HsWithdrawRecordController extends BaseController {

    private final IHsWithdrawRecordService iHsWithdrawRecordService;

    private final IKxUserBillService iKxUserBillService;

    private final IKxUserService userService;

    /**
     * 查询用户提现记录列表
     */
    @SaCheckPermission("huishou:withdrawRecord:list")
    @GetMapping("/list")
    public TableDataInfo<HsWithdrawRecordVo> list(HsWithdrawRecordBo bo, PageQuery pageQuery) {
        return iHsWithdrawRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户提现记录列表
     */
    @SaCheckPermission("huishou:withdrawRecord:export")
    @Log(title = "用户提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HsWithdrawRecordBo bo, HttpServletResponse response) {
        List<HsWithdrawRecordVo> list = iHsWithdrawRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户提现记录", HsWithdrawRecordVo.class, response);
    }

    /**
     * 获取用户提现记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("huishou:withdrawRecord:query")
    @GetMapping("/{id}")
    public R<HsWithdrawRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iHsWithdrawRecordService.queryById(id));
    }

    /**
     * 新增用户提现记录
     */
//    @SaCheckPermission("huishou:withdrawRecord:add")
//    @Log(title = "用户提现记录", businessType = BusinessType.INSERT)
//    @RepeatSubmit()
//    @PostMapping()
//    public R<Void> add(@Validated(AddGroup.class) @RequestBody HsWithdrawRecordBo bo) {
//        return toAjax(iHsWithdrawRecordService.insertByBo(bo));
//    }

    /**
     * 修改用户提现记录
     */
    @SaCheckPermission("huishou:withdrawRecord:edit")
    @Log(title = "用户提现记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @GetMapping("/edit")
    public R<Void> edit(HsWithdrawRecordBo bo) {
        if(bo.getId() == null || bo.getStatus() == null){
            return R.fail("参数错误！");
        }

        HsWithdrawRecordVo hsWithdrawRecordVo = iHsWithdrawRecordService.queryById(bo.getId());
        if(hsWithdrawRecordVo == null){
            return R.fail("提现记录不存在！");
        }

        if(hsWithdrawRecordVo.getStatus() != 0){
            return R.fail("记录已经审核，无需在审！");
        }

        if(bo.getStatus() == 1){
            //审核通过
        }
        else if(bo.getStatus() == 2){

            //审核不通过，返回余额
            KxUserVo kxUserVo = userService.queryById(hsWithdrawRecordVo.getUserId());
            if(kxUserVo == null){
                return R.fail("提现用户不存在！");
            }
            KxUserBo kxUserBo = new KxUserBo();
            kxUserBo.setNowMoney(kxUserVo.getNowMoney().add(hsWithdrawRecordVo.getAmount()));
            kxUserBo.setUid(kxUserVo.getUid());
            userService.updateByBo(kxUserBo);



            KxUserBillBo kxUserBillBo = new KxUserBillBo();
            kxUserBillBo.setIsDel(0);
            kxUserBillBo.setUid(hsWithdrawRecordVo.getUserId());
            kxUserBillBo.setLinkId(hsWithdrawRecordVo.getId()+"");
            kxUserBillBo.setPm(1);
            kxUserBillBo.setTitle("提现金额"+hsWithdrawRecordVo.getAmount()+"回退");
            kxUserBillBo.setCategory(BillDetailEnum.CATEGORY_1.getValue());
            kxUserBillBo.setType(BillDetailEnum.TYPE_13.getValue());
            kxUserBillBo.setNumber(hsWithdrawRecordVo.getAmount());
            kxUserBillBo.setBalance(kxUserBo.getNowMoney());
            kxUserBillBo.setStatus(1);

            iKxUserBillService.insertByBo(kxUserBillBo);


        }else if(bo.getStatus() == 3){

        }
        else {
            return R.fail("审核参数错误！");
        }

        HsWithdrawRecordBo hsWithdrawRecord = new HsWithdrawRecordBo();
        hsWithdrawRecord.setId(hsWithdrawRecordVo.getId());
        hsWithdrawRecord.setStatus(bo.getStatus());
        hsWithdrawRecord.setRemark(bo.getRemark());

        return toAjax(iHsWithdrawRecordService.updateByBo(hsWithdrawRecord));
    }

    /**
     * 删除用户提现记录
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("huishou:withdrawRecord:remove")
//    @Log(title = "用户提现记录", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}")
//    public R<Void> remove(@NotEmpty(message = "主键不能为空")
//                          @PathVariable Long[] ids) {
//        return toAjax(iHsWithdrawRecordService.deleteWithValidByIds(Arrays.asList(ids), true));
//    }
}
